package danny.hoi.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import danny.hoi.domain.export.cargo.*;
import danny.hoi.domain.export.vo.ExportProductVo;
import danny.hoi.domain.export.vo.ExportResult;
import danny.hoi.domain.export.vo.ExportVo;
import danny.hoi.service.cargo.ContractService;
import danny.hoi.service.cargo.ExportProductService;
import danny.hoi.service.cargo.ExportService;
import danny.hoi.web.controller.base.BaseController;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {
    @Reference
    private ExportService exportService;
    @Reference
    private ContractService contractService;
    @Reference
    private ExportProductService exportProductService;

    /**
     * 1.访问合同显示页面  只显示购销合同状态为1的记录
     * 2.请求地址：/cargo/export/contractList.do
     * 3.响应地址：WEB-INF/pages/cargo/export/export-contractList.jsp
     */
    @RequestMapping("/contractList")
    public String contractList(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize) {
        ContractExample example = new ContractExample();
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        criteria.andStateEqualTo(1);
        PageInfo<Contract> pageInfo = contractService.findByPages(example, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/export/export-contractList";
    }

    /**
     * 进入报运单页面
     * 1.请求地址：/cargo/export/list.do
     * 2.响应地址：WEB-INF/pages/cargo/export/export-list.jsp
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        ExportExample example = new ExportExample();
        example.setOrderByClause("create_time desc");
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo<Export> pageInfo = exportService.findByPages(example, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/export/export-list";
    }

    /**
     * 删除
     * 请求地址:/cargo/export/delete.do?id="+id;
     * 响应地址：redirect:/cargo/export/list.do
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        exportService.delete(id);
        return "redirect:/cargo/export/list.do";
    }

    /**
     * 访问报运页面
     * 请求地址:/cargo/export/toExport.do
     * 响应地址：WEB-INF/pages/cargo/export/export-toExport.jsp
     */
    @RequestMapping("/toExport")
    public String toExport(String id) {
        request.setAttribute("id", id);
        return "cargo/export/export-toExport";
    }

    /**
     * 新增/编辑
     * 请求地址：/cargo/export/edit.do
     * 响应地址：redirect:/cargo/export/list.do
     */
    @RequestMapping("/edit")
    public String edit(Export export) {
        export.setCompanyId(getLoginCompanyId());
        export.setCompanyName(getLoginCompanyName());
        export.setInputDate(new Date());
        if (StringUtils.isEmpty(export.getId())) {
            //新增
            export.setCreateBy(getLoginUser().getUserId());
            export.setCreateDept(getLoginUser().getDeptId());
            export.setCreateTime(new Date());
            exportService.save(export);
        } else {
            //更新
            exportService.update(export);
        }

        return "redirect:/cargo/export/list.do";
    }

    /**
     * 访问修改页面
     * 请求地址：/cargo/export/toUpdate.do?id=${o.id}
     * 响应地址：WEB-INF/pages/cargo/export/export-update.jsp
     */
    @RequestMapping("toUpdate")
    public String toUpdate(String id) {
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        //查询当前货运单下的商品
        ExportProductExample epExample = new ExportProductExample();
        ExportProductExample.Criteria epCriteria = epExample.createCriteria();
        epCriteria.andExportIdEqualTo(id);
        List<ExportProduct> eps = exportProductService.findAll(epExample);
        request.setAttribute("eps", eps);
        return "cargo/export/export-update";
    }

    /**
     * 查看购销合同
     * 请求地址：/cargo/export/toView.do
     * 响应地址：WEB-INF/pages/cargo/export/export-view.jsp
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        return "cargo/export/export-view";
    }

    /**
     * 上报
     * 请求地址：/cargo/export/submit.do
     * 响应地址：return "redirect:/cargo/export/list.do";
     */
    @RequestMapping("/submit")
    public String submit(String id) {
        exportService.submitState(id);
        return "redirect:/cargo/export/list.do";
    }

    /**
     * 取消上报
     * 请求地址： /cargo/export/cancel.do
     * 响应地址： return "redirect:/cargo/export/list.do";
     */
    @RequestMapping("/cancel")
    public String cancel(String id) {
        exportService.cancelState(id);
        return "redirect:/cargo/export/list.do";
    }

    /**
     * 电子报运
     * 请求地址：/cargo/export/exportE.do
     * 响应地址："redirect:/cargo/export/list.do";
     */
    @RequestMapping("/exportE")
    public String exportE(String id) {
        //查询电子报运的报运单
        Export export = exportService.findById(id);
        ExportVo exportVo = new ExportVo();
        //拷贝报运单数据
        BeanUtils.copyProperties(export, exportVo);
        exportVo.setExportId(id);
        //根据报运单id查询报运商品
        ExportProductExample epExample = new ExportProductExample();
        epExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> eps = exportProductService.findAll(epExample);
        if (eps != null && eps.size() > 0) {
            List<ExportProductVo> products = exportVo.getProducts();
            for (ExportProduct ep : eps) {
                ExportProductVo epo = new ExportProductVo();
                //拷贝商品数据
                BeanUtils.copyProperties(ep, epo);
                epo.setExportProductId(ep.getId());
                epo.setExportId(id);
                products.add(epo);
            }
        }
        //发送报运单到海关平台
        WebClient.create("http://127.0.0.1:8082/ws/exportService/user")
                .post(exportVo);

        //获取报运结果
        WebClient.create("http://127.0.0.1:8082/ws/exportService/user/" + id)
                .get(ExportResult.class);

        return "redirect:/cargo/export/list.do";
    }
}
